package com.hong.qrpc.handler.factory;

import com.hong.qrpc.constant.ProtoTypeConst;
import com.hong.qrpc.handler.in.request.QrpcRequestHandler;
import com.hong.qrpc.handler.in.response.QrpcResponseHandler;
import com.hong.qrpc.task.NettyTask;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;

import java.io.InputStream;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author HQT
 * @Date 2022/8/9 20:32
 */
public class HandlerFactory {
    public static ChannelInboundHandlerAdapter getRequestHandler(int protocolType, com.hong.qrpc.RequestHandler handler, SocketChannel socketChannel) {
        switch (protocolType) {
            case ProtoTypeConst.QRPC:
                return new QrpcRequestHandler(handler, socketChannel);
            default:
                throw new IllegalArgumentException("unsupported protocolType" + protocolType);
        }
    }

    public static ChannelInboundHandlerAdapter getResponseHandler(int protocolType, ConcurrentHashMap<Long, NettyTask<InputStream>> resultMap) {
        switch (protocolType) {
            case ProtoTypeConst.QRPC:
                return new QrpcResponseHandler(resultMap, protocolType);
            default:
                throw new IllegalArgumentException("unsupported protocolType" + protocolType);
        }
    }
}
